package team.seekers.hula.question.question.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import team.seekers.hula.common.entity.PageResult;
import team.seekers.hula.common.entity.VisibilityUpdateDTO;
import team.seekers.hula.record.mind.service.MindService;
import team.seekers.hula.question.point.service.PointManageService;
import team.seekers.hula.question.question.entity.dao.QuestionDO;
import team.seekers.hula.question.question.entity.dto.QuestionQueryDTO;
import team.seekers.hula.question.question.entity.dto.QuestionUpdateDTO;
import team.seekers.hula.question.question.entity.vo.QuestionManageListVO;
import team.seekers.hula.question.question.mapper.QuestionMapper;
import team.seekers.hula.auth.service.MindUtils;
import team.seekers.hula.user.user.service.UserService;

import java.util.*;

/**
 * 一个问题服务实现类
 * @author hbc
 * @since 2021-02-15
 */
@Service
public class QuestionManageService extends ServiceImpl<QuestionMapper, QuestionDO> {

    @Autowired
    @Lazy
    private UserService userService;

    @Autowired
    @Lazy
    private MindService mindService;

    @Autowired
    @Lazy
    private PointManageService pointManageService;


    // 管理式删除（无条件直接删光）
    @Transactional(rollbackFor=Exception.class)
    public void delete(String id) {
        // 删除所有的观点
        pointManageService.deleteByQuestionId(id);

        // 维护mind表
        mindService.deleteMindByLinkId(id);

        // 删除问题
        removeById(id);
    }


    @Transactional(rollbackFor=Exception.class)
    public void updateInfo(QuestionUpdateDTO updateVO) {
        QuestionDO questionDO = getById(updateVO.getId());
        BeanUtils.copyProperties(updateVO, questionDO);
        updateById(questionDO);
    }

    @Transactional(rollbackFor=Exception.class)
    public void updateVisibility(VisibilityUpdateDTO updateDTO) {
        lambdaUpdate().set(QuestionDO::getVisibility,updateDTO.getVisibility())
                .eq(QuestionDO::getId,updateDTO.getId())
                .update();

        mindService.updateVisibilityByLinkId(updateDTO.getId(),updateDTO.getVisibility());
    }


    public PageResult<QuestionManageListVO> page(QuestionQueryDTO questionQuery) {
        Page<QuestionDO> page = new Page<>(questionQuery.getPageNow(),questionQuery.getPageSize());
        QueryWrapper<QuestionDO> wrapper = new QueryWrapper<>();

        if(StringUtils.isNotBlank(questionQuery.getTitle())){
            wrapper.like("title",questionQuery.getTitle());
        }
        if (MindUtils.isTrueType(questionQuery.getType())){
            wrapper.eq("type",questionQuery.getType());
        }
        if(questionQuery.getStatus()!=null){
            wrapper.eq("status",questionQuery.getStatus());
        }
        if(StringUtils.isNotBlank(questionQuery.getContext())){
            wrapper.like("context",questionQuery.getContext());
        }

        page(page,wrapper);
        if(page.getTotal() == 0)
            return new PageResult<>();

        List<QuestionManageListVO> list = new ArrayList<>();
        for (QuestionDO record : page.getRecords()) {
            QuestionManageListVO questionListVO = new QuestionManageListVO();
            BeanUtils.copyProperties(record,questionListVO);
            if(record.getIsHot() == 1){
                questionListVO.setIsHot(true);
            }
            if(record.getIsSpecial() == 1){
                questionListVO.setIsSpecial(true);
            }
            questionListVO.setStatus(record.getStatus().getType());
            questionListVO.setNickName(userService.getBasicInfoVO(record.getUserId()).getNickname());
            list.add(questionListVO);
        }
        return new PageResult<>(page.getTotal(),list);
    }


    public List<QuestionManageListVO> listManageQuestion(String userId) {
        // TODO 根据用户id来筛选
        List<QuestionDO> questionDOS = list();
        if(questionDOS.size() == 0)
            return new ArrayList<>();

        List<QuestionManageListVO> list = new ArrayList<>();
        for(QuestionDO questionDO : questionDOS){
            QuestionManageListVO questionVo = new QuestionManageListVO();
            BeanUtils.copyProperties(questionDO,questionVo);
            list.add(questionVo);
        }
        return list;
    }
}
